-------------------------------------------------------------------------------------
-- -*- coding: utf-8 -*-
--
-- @author:   ztw
-- @email:    986241885@qq.com
-- @date:     2024/7/2
-- @module:   transfer_gold
-- @describe:
-- @version:  v1.0
--

-------------------------------------------------------------------------------------

---@class transfer_gold
local transfer_gold = {}
local this = transfer_gold
---@type redis
local redis = import("game/func/redis")
---@type item
local item = import("game/func/item")
---@type configer
local configer = import("base/configer")
local trace = trace
local local_player = local_player

-------------------------------------------------------------------------------------
-- [读取] 获取指定仓库名 转金记录数据(此命令 可供收金号读取)
-- 参数1：仓库收金名
-------------------------------------------------------------------------------------
function this.get_transfer_data_by_warehouse_name_in_redis(transfer_path, warehouse_name, owner)
    local path = transfer_path
    --保存redis读取的数据
    local ret = {}
    -- 物品名
    local item_name = owner.transferGoldData["材料名"]
    -- 单价
    local price = owner.transferGoldData["上架单价"]
    --读取指定名 或本身 名下 转金总记录
    warehouse_name = warehouse_name or local_player:name()
    for i = 1, (owner.transferGoldData.MAX_POS_TRANSFER or 30) do
        local ret_client = redis.get_json_redis_by_path(path .. warehouse_name .. ':' .. i)
        ret_client["交易物品名"] = item_name
        ret_client["上架单价"] = price
        ret_client.can_idx2 = i --当前存在记录
        ret_client.warehouse_name = warehouse_name
        table.insert(ret, ret_client)
    end
    --print_r(ret)
    return ret
end

-------------------------------------------------------------------------------------
-- 获取需要转金的记录 data (收金号执行<上架物品>时使用)
-------------------------------------------------------------------------------------
function this.get_need_transfer_gold_data_in_redis(is_only_status, transfer_path, warehouse_name, owner)
    local ret = this.get_transfer_data_by_warehouse_name_in_redis(transfer_path, warehouse_name, owner)
    --print_r(ret)
    --保存需要上架的数据
    local return_ret = {}
    --xxmsg(item_name, price)
    for i = 1, #ret do
        local ret_client = ret[i]
        local t_gold_id = ret_client['转金号ID']
        local n_transfer_gold = ret_client['可转金币']
        local up_time = ret_client['活跃时间']
        if t_gold_id ~= 0 and t_gold_id ~= nil then
            if n_transfer_gold ~= 0 then
                -- 如果超过了10分钟则不获取
                -- 可转金币大于10
                if n_transfer_gold >= 10 or (not up_time) then
                    --print_r(ret)
                    if not is_only_status then
                        --if e_name == '' and not is_only_status then
                        table.insert(return_ret, ret_client)
                    end
                end
            end
        end
    end
    --print_r(return_ret)
    return return_ret
end

-------------------------------------------------------------------------------------
-- [读取] 获取可转金币[转金号]
-------------------------------------------------------------------------------------
function this.get_can_transfer_gold(owner)
    --return item_unit.get_money_byid(3) -
    if owner.transferGoldData['开启交易转金'] == 1 then
        return item_unit.get_money_byid(1) - owner.transferGoldData['转金保留钻石'] or 0
    end
    return 0
end


-------------------------------------------------------------------------------------
-- [读取] 获取最低转移金币[转金号]
-------------------------------------------------------------------------------------
function this.get_trigger_min_gold(owner)

    local gold_min = owner.transferGoldData["触发交易钻石下限"]
    local gold_max = owner.transferGoldData["触发交易钻石上限"]
    gold_min = tonumber(gold_min)
    gold_max = tonumber(gold_max)
    if gold_min > gold_max then
        trace.output("设置触发交易钻石阈值有误，请检查或重启脚本")
    end
    -- 角色当前服务器
    local need_server = main_ctx:c_server_name()
    local gold_min_ini = configer.get_user_profile(need_server, "触发交易钻石下限")
    --gold_min_ini = tonumber(gold_min_ini)
    -- 如果redis中设置的交易下限与上限与ini中配置不一样时，则更改为redis中设置的触发交易钻石下限或上限
    if gold_min_ini == nil or gold_min_ini ~= tostring(gold_min) then
        configer.set_user_profile(need_server, "触发交易钻石下限", gold_min)
        gold_min_ini = gold_min
    end
    local gold_max_ini = configer.get_user_profile(need_server, "触发交易钻石上限")
    --gold_max_ini = tonumber(gold_max_ini)
    if gold_max_ini == nil or gold_max_ini ~= tostring(gold_max) then
        configer.set_user_profile(need_server, "触发交易钻石上限", gold_max)
        gold_max_ini = gold_max
    end

    -- 随机生成触发交易钻石的金额
    local can_transfer_gold = redis.get_user_profile_today_four(need_server, "触发交易钻石")
    -- 如果读取的触发交易钻石为空或者下限或者上限被修改时，重新随机生成金额
    gold_min_ini = tonumber(gold_min_ini)
    gold_max_ini = tonumber(gold_max_ini)
    if not gold_min_ini or not gold_max_ini then
        return 10, "获取上限或下限为空"
    end
    if can_transfer_gold == "" or gold_min_ini ~= gold_min or gold_max_ini ~= gold_max then
        math.randomseed(os.time())
        can_transfer_gold = math.random(gold_min, gold_max)
        redis.set_user_profile_today_four(need_server, "触发交易钻石", can_transfer_gold)
    end
    can_transfer_gold = tonumber(can_transfer_gold)
    return can_transfer_gold or 10
end

-------------------------------------------------------------------------------------
-- [行为] 出金号 刷新转金位置 记录[转金号]
-- 返回：当前上架仓库名,上架记录位置,当前位置所有数据,是否存在记录
-------------------------------------------------------------------------------------
function this.refresh_upload_transfer_gold_in_redis(is_clear, owner, warehouse_path, transfer_path)
    local id = local_player:id()
    if id <= 0 then
        --读取ID 异常返回
        return '', 0, {}, false, ''
    end
    local count = owner.transferGoldData['最大收金号数'] or 40 --收金号数
    local name = local_player:name() --角色名称
    local ret = {} --保存从redis读取到的数据
    local ret2 = {} --保存所有活跃仓库  所有上架记录数
    local can_idx_name = '' --记录 仓库
    local can_idx2 = 0 --记录指定仓库名下所在位置
    local out_time = 1800 --活跃超时时间 当前仓库名 转金记录 最大超时时间  默认 半小时
    local is_my_cord = false --记录是否已存在
    local gold = this.get_can_transfer_gold(owner) --读取当前可转金币数
    local path_new = warehouse_path
    local transfer_gold_limit = owner.transferGoldData["单笔交易上限"]
    transfer_gold_limit = tonumber(transfer_gold_limit)
    transfer_gold_limit = transfer_gold_limit or 1000
    -- 如果可转金额大于单笔交易上限，则只转单笔交易上限
    if gold > transfer_gold_limit then
        gold = transfer_gold_limit
    end
    --保存所有 的仓库数据 到ret
    for i = 1, count do
        local ret_client = redis.get_json_redis_by_path(path_new .. i, nil)
        table.insert(ret, ret_client)
    end

    --保存 所有活跃的仓库  仓库 名下对应 转金记录 数据 到 ret2
    for i = 1, #ret do
        local ret_client = ret[i]
        if not table.is_empty(ret_client) then
            local brisk_time = ret_client['活跃时间']
            if brisk_time ~= nil then
                if os.time() - brisk_time < out_time then
                    local warehouse_name = ret_client['收金号']

                    local ret_client1 = this.get_transfer_data_by_warehouse_name_in_redis(transfer_path, warehouse_name,
                        owner)
                    for j = 1, #ret_client1 do
                        ret_client1[j].can_idx_name = warehouse_name
                        table.insert(ret2, ret_client1[j])
                    end
                end
            end
        end
    end
    --print_r(ret2)
    --读取 本角色  可刷新的位置
    for i = 1, #ret2 do
        local ret_client1 = ret2[i]
        if ret_client1['转金号ID'] == nil then
            if can_idx2 == 0 then
                can_idx_name = ret_client1.can_idx_name
                can_idx2 = ret_client1.can_idx2 --can_idx2
            end
        else
            if ret_client1['转金号'] == name or ret_client1['转金号ID'] == tostring(id) then
                can_idx_name = ret_client1.can_idx_name
                can_idx2 = ret_client1.can_idx2
                is_my_cord = true
                break
            end
        end
    end

    --读取本角色可刷新的位置
    if not is_my_cord and can_idx2 == 0 then
        for i = 1, #ret2 do
            local ret_client1 = ret2[i]
            if ret_client1['转金号ID'] ~= nil then
                local brisk_time = ret_client1['活跃时间']
                if type(brisk_time) == 'number' then
                    if os.time() - brisk_time > out_time then
                        can_idx_name = ret_client1.can_idx_name
                        can_idx2 = ret_client1.can_idx2
                        break
                    end
                end
            end
        end
    end
    --xxmsg("transfer_path")
    --xxmsg(transfer_path)
    -- 转金号在转移路径下写入数据
    local data_r = {} --当前位置所有key 数据
    local path2 = transfer_path
    if can_idx2 > 0 and can_idx_name ~= '' and gold >= this.get_trigger_min_gold(owner) and not is_clear then
        local data = {}
        -- 判断转移路径下是否有本角色的数据，没有则写入默认值
        --xxmsg(is_my_cord)
        if is_my_cord then
            --if true then
            --xxmsg((path2 .. can_idx_name .. ':' .. can_idx2))
            data = redis.get_json_redis_by_path(path2 .. can_idx_name .. ':' .. can_idx2, nil)
            --data = redis_ent.get_json_redis_by_path(path2 .. can_idx_name .. ':' .. "3", this.CONNECT_OBJ)
            data['可转金币'] = gold
            --print_r(data)
        else
            data['转金号'] = name
            --data['转金号'] = "용은채96"
            data['转金号ID'] = tostring(id)
            data['交易行物品ID'] = '0'
            data['交易物品名'] = ''
            data['交易行资源ID'] = 0
            data['交易行上架时'] = 0
            data['可转金币'] = gold
            data['交易上架状态'] = -1
            data['是否装备'] = 0
            data['装备等级'] = 0
        end
        data['活跃时间'] = os.time()
        data['日期'] = os.date("%Y-%m-%d,%H:%m:%S")
        data['time'] = os.time()
        data_r = data

        redis.set_json_redis_by_path_and_data(path2 .. can_idx_name .. ':' .. can_idx2, data)
    end
    --print_r(data_r)
    return can_idx_name, can_idx2, data_r, is_my_cord, (path2 .. can_idx_name .. ':' .. can_idx2)
end

return this
